<template>
    <div class="app-container">
        <!-- 查询模块 -->
        <div class="formDiv">
            <el-form ref="form" :inline="true" :model="form" label-width="130px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="付款单号">
                            <el-input v-model="form.id" placeholder="请输入付款单号"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="回款单号">
                            <el-input v-model="form.repaymentSn" placeholder="请输入回款单号"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="手机号">
                            <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6" v-if="hasPermission('adminRole')">
                        <el-form-item label="医院名称">
                            <el-select v-model="form.hospitalId" placeholder="请选择医院" filterable>
                                <el-option
                                    :label="item.name"
                                    :value="item.value"
                                    v-for="item in hospitalList"
                                    :key="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="付款时间" width="236">
                            <el-date-picker
                                value-format="yyyy-MM-dd HH:mm:ss"
                                v-model="dates"
                                type="daterange"
                                range-separator="-"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期"
                                :default-time="['00:00:00', '23:59:59']"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="18" class="alignRight" v-if="hasPermission('adminRole')">
                        <el-button type="primary" class="queryBtn" @click="handleSearch">查询</el-button>
                        <el-button class="leadingOut" type="primary" @click="handleExport">导出</el-button>
                    </el-col>
                    <el-col :span="24" class="alignRight" style="margin-bottom:20px" v-else>
                        <el-button type="primary" class="queryBtn" @click="handleSearch">查询</el-button>
                        <el-button class="leadingOut" type="primary" @click="handleExport">导出</el-button>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <!-- 主页列表 -->
        <div class="listDiv" v-loading="tableLoading">
            <!-- table列表 -->
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column type="index" width="50px" label="序号"></el-table-column>
                <el-table-column prop="hospitalName" label="医院名称"></el-table-column>
                <el-table-column prop="drawbackSn" label="付款单号"></el-table-column>
                <el-table-column prop="repaymentSn" label="回款单号"></el-table-column>
                <el-table-column prop="username" label="客户姓名"></el-table-column>
                <el-table-column prop="phone" label="客户手机号"></el-table-column>
                <el-table-column prop="receivableAmount" label="退款金额"></el-table-column>
                <el-table-column label="回款比率">
                    <template slot-scope="scope">
                        {{scope.row.backRatio}}%
                    </template>
                </el-table-column>
                <el-table-column prop="backAmount" label="应付款金额"></el-table-column>
                <el-table-column prop="paidAmount" label="实际付款金额"></el-table-column>
                <el-table-column prop="checkedAmount" label="核销金额"></el-table-column>
                <el-table-column prop="paidCard" label="付款银行卡账号"></el-table-column>
                <el-table-column prop="remark" label="备注"></el-table-column>
                <el-table-column prop="createdAt" label="付款时间"></el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="pagesCon">
                <div>
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="form.pageNum"
                        :page-sizes="page_sizes"
                        :page-size="form.limit"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="page_totals"
                    ></el-pagination>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import mmkPay from "@/api/mmkPay";
import commonFn from "@/utils/common";
export default {
    name: "mmkPayIndex",
    data() {
        return {
            // loading层
            tableLoading: true,
            //时间容器
            dates: [],
            //提交表单数据
            form: {
                pageNum: 1,
                pageSize: 10,
                repaymentSn: "",
                id: "",
                phone: "",
                hospitalId: "",
                startTime: "",
                endTime: ""
            },
            //table列表
            tableData: [],
            //医院名称下拉列表
            hospitalList: [],
            //页码
            page_sizes: [10, 30, 50, 100],
            page_totals: 60
        };
    },
    mounted() {
        if(!this.hasPermission('adminRole')){
            this.form.hospitalId = String(this.$store.state.user.hospitalId) 
        }
        this.getList(this.form);
        this.findHospitalList();
    },
    methods: {
        // 按钮权限
        hasPermission(e) {
            return window.helper.hasPermission(e);
        },
        //获取列表
        getList(e) {
            mmkPay
                .getList(e)
                .then(res => {
                    // console.log(res);
                    if (!res.data.code) {
                        let resData = res.data.data;
                        this.tableData = resData.paymentResponseDTOS;
                        this.page_totals = resData.count;
                    } else {
                        this.$message({
                            showClose: true,
                            message: res.data.msg,
                            type: "warning"
                        });
                    }
                    this.tableLoading = false
                })
                .catch(err => {
                    this.errorStatus(err);
                    this.tableLoading = false
                });
        },
        // 查询
        handleSearch() {
            this.form.pageNum = 1
            if (this.dates != null) {
                if (this.dates.length > 0) {
                    this.form.startTime = this.dates[0];
                    this.form.endTime = this.dates[1];
                } else {
                    this.form.startTime = null;
                    this.form.endTime = null;
                }
            } else {
                this.form.startTime = null;
                this.form.endTime = null;
            }
            this.getList(this.form);
        },
        // 导出
        handleExport() {
            let listExport = { ...this.form };
            delete listExport.pageSize;
            delete listExport.pageNum;
            mmkPay
                .handleExport(listExport)
                .then(res => {
                    commonFn.downFile("退款单", res);
                })
                .catch(err => {
                    this.errorStatus(err);
                });
        },
        // 医院名称下拉列表
        findHospitalList() {
            mmkPay
                .findHospitalList()
                .then(res => {
                    // console.log(res);
                    if (!res.data.code) {
                        let resData = res.data.data;
                        let newArr = [];
                        resData.forEach(e => {
                            newArr.push({
                                name: e.name,
                                value: String(e.hospitalId)
                            });
                        });
                        newArr.unshift({
                            name: "全部",
                            value: ""
                        });
                        this.hospitalList = newArr;
                        // console.log(this.hospitalList);
                    } else {
                        this.$message({
                            showClose: true,
                            message: res.data.msg,
                            type: "warning"
                        });
                    }
                })
                .catch(err => {
                    this.errorStatus(err);
                });
        },
        //弹窗组件
        eleMsg(txt, status) {
            this.$message({
                showClose: true,
                message: txt,
                type: status
            });
        },
        //成功状态弹窗
        succStatus(txt, status) {
            this.eleMsg(txt, "success");
        },
        //失败状态弹窗
        errorStatus(txt) {
            this.eleMsg(txt, "error");
        },
        // 警告状态弹窗
        warningStatus(txt) {
            this.eleMsg(txt, "warning");
        },
        // 路由跳转
        toRoutes(e, userId) {
            let route = { name: e, query: { userId: userId } };
            this.$router.push(route);
        },
        //改变页面条数
        handleSizeChange(val) {
            this.form.pageSize = val;
            this.getList(this.form);
        },
        //选择页数
        handleCurrentChange(val) {
            this.form.pageNum = val;
            this.getList(this.form);
        }
    }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
    height: 40px;
    margin-top: 20px;
}
.pagesCon > div {
    float: right;
}
/* 表单样式修改 */
.el-range-editor--medium.el-input__inner {
    width: 236px;
}
/* 导出按钮样式 */
/* .leadingOut {
  float: right;
  margin: 15px;
} */
/* 查询按钮样式 */
/* .queryBtn {
  margin-left: 50%;
  transform: translateX(-50%);
} */
</style>